var $;
layui.use(['layer', 'form', 'jquery', 'table', 'laydate'], function () {
    var layer = layui.layer;
    var form = layui.form;
    $ = layui.jquery;
    var table = layui.table;
    var laydate = layui.laydate;
    //注册时间
    laydate.render({
        elem: '#createDate',
        type: 'date',
        trigger: 'click'
    });
    getTable();

    function getTable() {
        $.ajax({
            url: '/user/getZeroConsumeUser',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                tablereload(data.data);
            }
        });
    }

    var tableIns;

    function tablereload(data) {
        tableIns = table.render({
            elem: '#table',
            toolbar: true,
            data: data,
            async: true,
            title: '零消费用户列表',
            page: {
                limit: 10,
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: "首页",
                last: "尾页",
            },
            cols: [[
                {type: 'checkbox', with: 50},
                {
                    field: '', title: '   序号', align: 'center', templet: function (data) {
                        return data.LAY_INDEX;
                    }
                },
                {field: 'userCode', title: '用户编号', align: 'center'},
                {field: 'account', title: '用户账号', align: 'center'},
                {field: 'nickname', title: '用户昵称', align: 'center'},
                {
                    field: 'studyDate', title: '学习时长', align: 'center', templet: function (data) {
                        return data.studyDate + " 分钟";
                    }
                },
                {
                    field: 'consumption', title: '消费金额', align: 'center', templet: function (data) {
                        return data.consumption + " 元";
                    }
                },
                {field: 'createDate', title: '账户注册时间', align: 'center'},
                {
                    field: 'status', title: '账号状态', align: 'center', templet: function (data) {
                        if (data.status == 2) {
                            var radio = "<input type='checkbox' lay-skin='switch' name='status' disabled lay-skin='switch' lay-text='正常|停用' checked>";
                        } else {
                            var radio = "<input type='checkbox' lay-skin='switch' name='status' disabled lay-skin='switch' lay-text='正常|停用'>";
                        }
                        return radio;
                    }
                },
                {
                    fiele: '', title: '操作', align: 'center', templet: function (data) {
                        return "<div><a href='javascript:getinformation(" + data.userId + ");' style='margin-right: 10px;color: #009688'>查看</a><a href='javascript:updateBeforeUser(" + data.userId + ");' style='margin-right: 10px;color: #009688'>编辑</a><a href='javascript:shutDownBeforeUser(\"" + data.userId + "\",\"" + data.account + "\");' style='margin-right: 10px;color: #009688'>封停</a></div>";
                    }
                }
            ]]
        });
    }

    var userId;
    var userDev;
    table.on('checkbox(test)', function (obj) {
        userId = obj.data.userId;
        userDev = obj.data;
    });
    giveCoupons = function giveCoupons() {
        var checkStatus = table.checkStatus('table').data;
        if (checkStatus.length < 1) {
            layer.msg("请选择用户", {icon: 5});
        } else if (checkStatus.length == 1) {
            layer.open({
                type: 2,
                title: '优惠券数据列表',
                shade: 0,
                maxmin: true,
                offset: 'auto',
                area: ['1000px', '650px'],
                content: "/user/user-give-coupons.html",
                success: function (layero, index) {
                    var obj = layero.find('iframe').contents();
                    obj.find('input[name=userId]').val(userId);
                }
            });
        } else {
            layer.msg("请选择一个用户", {icon: 5});
        }
    }
    //查看
    getinformation = function getinformation(userId) {
        $.ajax({
            url: '/user/getNonUserDevelopmentById?consumption=0',
            type: 'post',
            dataType: 'json',
            data: {'userId': userId},
            success: function (data) {
                devicesMsg(data.data);
            }
        });
    }

    function devicesMsg(data) {
        layer.open({
            type: 2,
            title: '用户信息',
            offset: 'auto',
            shade: 0,
            maxmin: true,
            area: ['1000px', '790px'],
            content: '/user/user-not-jinsi-see.html',
            btn: ['关闭'],
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('#avatarUrl').attr('src', data.avatarUrl);
                obj.find('input[name=account]').val(data.account);
                obj.find('input[name=nickname]').val(data.nickname);
                obj.find('input[name=userCode]').val(data.userCode);
                obj.find('input[name=studyDate]').val(data.studyDate);
                obj.find('input[name=freestyle]').val(data.freestyle);
                obj.find('input[name=idCard]').val(data.idCard);
                obj.find('input[name=phone]').val(data.phone);
                obj.find('input[name=sex]').val(data.sex);
                obj.find('input[name=email]').val(data.email);
                var consumption = data.consumption + " 元";
                obj.find('input[name=consumption]').val(consumption);
            }
        });
    }

    //编辑
    updateBeforeUser = function updateBeforeUser(userId) {
        $.ajax({
            url: '/user/getNonUserDevelopmentById?consumption=0',
            type: 'post',
            dataType: 'json',
            data: {'userId': userId},
            success: function (data) {
                updateMsg(data.data);
            }
        });
    }

    function updateMsg(data) {
        layer.open({
            type: 2,
            title: '修改用户信息',
            offset: 'auto',
            shade: 0,
            maxmin: true,
            area: ['1000px', '700px'],
            content: '/user/user-update.html',
            btn: ['修改', '取消'],
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('#avatarUrl').attr('src', data.avatarUrl);
                obj.find('input[name=avatarUrl]').val(data.avatarUrl);
                obj.find('input[name=userId]').val(data.userId);
                obj.find('input[name=account]').val(data.account);
                obj.find('input[name=nickname]').val(data.nickname);
                obj.find('input[name=freestyle]').val(data.freestyle);
                obj.find('input[name=idCard]').val(data.idCard);
                obj.find('input[name=phone]').val(data.phone);
                obj.find('input[name=email]').val(data.email);
                obj.find('input[name=sex]').val(data.sex);
                var consumption = data.consumption + '元       （不可修改）';
                obj.find('input[name=consumption]').val(consumption);
                var studyDate = data.studyDate + '分钟       （不可修改）';
                obj.find('input[name=studyDate]').val(studyDate);
                var createDate = data.createDate + ' （不可修改）';
                obj.find('input[name=createDate]').val(createDate);
            },
            yes: function (index, layero) {
                var obj = layero.find('iframe').contents();
                //获取form中的信息
                var param = obj.find('#form').serialize();
                $.ajax({
                    url: '/user/updateBeforeUser',
                    type: 'post',
                    dataType: 'json',
                    data: param,
                    success: function (data) {
                        layer.msg("修改成功");
                        getTable();
                    }
                });
                layer.close(index);
            }
        });
    }

    //封停用户
    shutDownBeforeUser = function shutDownBeforeUser(userId, account) {
        layer.open({
            type: 2,
            title: '封停用户',
            offset: 'auto',
            shade: 0,
            maxmin: true,
            area: ['400px', '360px'],
            content: '/user/user-shut-down.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('input[name=account]').val(account);
            },
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                var obj = layero.find('iframe').contents();
                var days = obj.find('#days option:selected').val();
                var reason = obj.find('#reason').html();
                $.ajax({
                    url: '/user/updateStatus',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        'userId': userId,
                        'status': 5
                    }
                });
                var closure = {
                    'userId': userId,
                    'type': 2,
                    'reason': reason,
                    'days': days,
                }
                $.ajax({
                    url: '/closure/addClosure',
                    type: 'post',
                    dataType: 'json',
                    data: closure,
                    success: function (data) {
                        layer.msg("封停成功");
                        getTable();
                        layer.close(index);
                    }
                });
            }
        });
    }
    //搜索
    form.on('submit(search)', function (data) {
        $.ajax({
            url: '/user/getNonUserDevelopment?consumption=0',
            type: 'post',
            dataType: 'json',
            data: data.field,
            success: function (data) {
                tablereload(data.data);
            }
        });
    });
});

//跳转
function goBack() {
    //通过页面查找iframe
    parent.$('#recordLog').val($('#zero').text());
    parent.before();
    return false;
}